using System;
using UnityEngine;
using UnityEngine.UI;

namespace MaterialUI
{
	public class MaterialAppBar : MonoBehaviour
	{
		public enum ButtonElement
		{
			Text,
			Icon,
			BackgroundImage
		}

		[Tooltip("The title text on the AppBar.")]
		[SerializeField]
		private Text m_TitleText;

		[Tooltip("The background graphic of the panel.")]
		[SerializeField]
		private Graphic m_PanelGraphic;

		[Tooltip("The AnimatedShadow controlling the shadow's state.")]
		[SerializeField]
		private AnimatedShadow m_Shadow;

		[Tooltip("Any other buttons on the AppBar, for color control.")]
		[SerializeField]
		private MaterialButton[] m_Buttons;

		[Tooltip("[Required] The duration of animations controlled by this component.")]
		[SerializeField]
		private float m_AnimationDuration = 0.5f;

		[Tooltip("[Required] The graphic of the shadow.")]
		private Graphic m_ShadowGraphic;

		public Text titleText
		{
			get
			{
				return m_TitleText;
			}
			set
			{
				m_TitleText = value;
			}
		}

		public Graphic panelGraphic
		{
			get
			{
				return m_PanelGraphic;
			}
			set
			{
				m_PanelGraphic = value;
			}
		}

		public AnimatedShadow shadow
		{
			get
			{
				return m_Shadow;
			}
			set
			{
				m_Shadow = value;
			}
		}

		public MaterialButton[] buttons
		{
			get
			{
				return m_Buttons;
			}
			set
			{
				m_Buttons = value;
			}
		}

		public float animationDuration
		{
			get
			{
				return m_AnimationDuration;
			}
			set
			{
				m_AnimationDuration = value;
			}
		}

		public Graphic shadowGraphic
		{
			get
			{
				if (m_ShadowGraphic == null)
				{
					m_ShadowGraphic = m_Shadow.GetComponent<Graphic>();
				}
				return m_ShadowGraphic;
			}
		}

		public void SetPanelColor(Color color, bool animate = true)
		{
			bool flag = color.a < 1f;
			m_Shadow.SetShadow(!flag, flag ? Tween.TweenType.EaseOutQuint : Tween.TweenType.SoftEaseOutQuint, !animate);
			Color color2 = m_PanelGraphic.color;
			if (color2.a < 0.015f)
			{
				Graphic panelGraphic = m_PanelGraphic;
				Color color3 = color;
				Color color4 = m_PanelGraphic.color;
				panelGraphic.color = color3.WithAlpha(color4.a);
			}
			if (animate && Application.isPlaying)
			{
				Action<Color> updateValue = delegate(Color c)
				{
					m_PanelGraphic.color = c;
					Graphic shadowGraphic2 = this.shadowGraphic;
					Color color7 = m_PanelGraphic.color;
					Color color8 = this.shadowGraphic.color;
					shadowGraphic2.color = color7.WithAlpha(color8.a);
				};
				TweenManager.TweenValue(updateValue, m_PanelGraphic.color, color, m_AnimationDuration);
				return;
			}
			m_PanelGraphic.color = color;
			Graphic shadowGraphic = this.shadowGraphic;
			Color color5 = m_PanelGraphic.color;
			Color color6 = this.shadowGraphic.color;
			shadowGraphic.color = color5.WithAlpha(color6.a);
		}

		public void SetTitleTextColor(Color color, bool animate = true)
		{
			if (m_TitleText != null)
			{
				TweenGraphicColor(m_TitleText, color, animate);
			}
		}

		public void SetButtonGraphicColor(int buttonIndex, Color color, ButtonElement element, bool animate = true)
		{
			MaterialButton button = m_Buttons[buttonIndex];
			if (button != null)
			{
				object obj;
				switch (element)
				{
				case ButtonElement.Text:
					obj = button.text;
					break;
				case ButtonElement.Icon:
					obj = button.icon;
					break;
				default:
					obj = button.backgroundImage;
					break;
				}
				Graphic graphic = (Graphic)obj;
				if (!(graphic == null))
				{
					TweenGraphicColor(graphic, color, animate, delegate
					{
						button.RefreshRippleMatchColor();
					});
				}
			}
		}

		public void SetButtonsGraphicColors(Color color, ButtonElement element, bool animate = true)
		{
			for (int i = 0; i < m_Buttons.Length; i++)
			{
				SetButtonGraphicColor(i, color, element, animate);
			}
		}

		private void TweenGraphicColor(Graphic graphic, Color color, bool animate, Action onTweenEndAction = null)
		{
			if (animate && Application.isPlaying)
			{
				TweenManager.TweenValue(delegate(Color c)
				{
					graphic.color = c;
				}, graphic.color, color, m_AnimationDuration, 0f, onTweenEndAction);
				return;
			}
			graphic.color = color;
			onTweenEndAction.InvokeIfNotNull();
		}
	}
}
